﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace Reservi
{
    class Game
    {
        private const int Cols = 8;
        private const int Rows = 8;
        private bool NextIsBlack;
        public int Num_Black { get; set; }
        public int Num_White { get; set; }
        public int Count { get; set; }       
       
        /// <summary>
        /// 所有块类的集合
        /// </summary>
        private List<Block> Blocks { get; set; }

        /// <summary>
        /// 初始化
        /// </summary>
        public Game()
        {
            NextIsBlack = true;
            //初始化集合            
            Blocks = new List<Block>();
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                {
                    Blocks.Add(new Block(i, j));
                }
            }
            //初始化四个棋子                       
            SetBlock(3, 4, BlockStatus.White);
            SetBlock(4, 3, BlockStatus.White);
            SetBlock(3, 3, BlockStatus.Black);
            SetBlock(4, 4, BlockStatus.Black);
            //初始化计数
            Num_Black = 2;
            Num_White = 2;
        }

        /// <summary>
        /// 初始棋子
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="status"></param>
        public void SetBlock(int x, int y, BlockStatus status)
        {
            Blocks.First(m => m.Point == new Point(x, y)).Status = status;
        }

        /// <summary>
        /// 绘制当前游戏场景
        /// </summary>
        /// <param name="g">绘图句柄</param>
        /// <param name="size">游戏区域尺寸</param>
        public void Draw(Graphics g, Size size)
        {
            //画背景
            Rectangle rec = new Rectangle(new Point(0, 0), size);
            g.DrawImage(Properties.Resources.bg, rec);
            //画格子
            int unitColWidth = size.Width / (Cols + 2);
            int unitRowHeight = size.Height / (Rows + 2);
            //横线
            for (int i = 0; i <= Rows; i++)
            {
                Point pointStart = new Point(unitColWidth, (i + 1) * unitRowHeight);
                Point pointEnd = new Point(unitColWidth*(Cols+1), (i + 1) * unitRowHeight);
                g.DrawLine(new Pen(Color.Black), pointStart, pointEnd);
            }
            //竖线
            for (int i = 0; i <= Cols; i++)
            {
                Point pointStart = new Point((i + 1) * unitColWidth, unitRowHeight);
                Point pointEnd = new Point((i + 1) * unitColWidth, unitRowHeight*(Rows+1));
                g.DrawLine(new Pen(Color.Black), pointStart, pointEnd);
            }
            //画棋子
            foreach (var block in Blocks)
            {
                block.Draw(g, new Rectangle
                {
                    Size = new Size((int)unitColWidth, (int)unitRowHeight),
                    Location = new Point((int)((block.Point.Y+1) * unitColWidth), (int)((block.Point.X+1) * unitRowHeight))
                });
            }
            //画黑白棋个数
            Font drawFont = new Font("Arial", 10);
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            g.DrawString("黑:" + Num_Black.ToString(), drawFont, drawBrush, 0, 0);
            g.DrawString("白:" + Num_White.ToString(), drawFont, drawBrush, 0, 20);
        }           
                
        /// <summary>
        /// 处理鼠标事件
        /// </summary>
        /// <param name="point"></param>
        /// <param name="size"></param>
        public Block MouseDown(Point point, Size size)
        {
            //格子长度宽度
            int unitColWidth = size.Width / (Cols + 2);
            int unitRowHeight = size.Height / (Rows + 2);
            
            foreach (var block in Blocks)
            {                
                var colorNow = NextIsBlack ? BlockStatus.Black : BlockStatus.White;
                var colorOther = NextIsBlack ? BlockStatus.White : BlockStatus.Black;
                //检索右侧
                var blockRight = Blocks.Where(m => m.Point.X == block.Point.X
                    && m.Point.Y > block.Point.Y
                    && m.Status == colorNow).OrderBy(m => m.Point.Y).FirstOrDefault();
                if (blockRight != null)
                {
                    var blocksMiddle = Blocks.Where(m => m.Point.X == block.Point.X
                          && m.Point.Y > block.Point.Y
                          && m.Point.Y < blockRight.Point.Y);
                    if (blocksMiddle.Count() > 0 && !blocksMiddle.Any(m => m.Status != colorOther))
                    {
                        Count += blocksMiddle.Count();                        
                        foreach (var item in blocksMiddle)
                        {
                            item.Status = colorNow;
                        }
                    }
                }
                //检索左侧
                var blockLeft = Blocks.Where(m => m.Point.X == block.Point.X
                    && m.Point.Y < block.Point.Y
                    && m.Status == colorNow).OrderBy(m => m.Point.Y).FirstOrDefault();
                if (blockLeft != null)
                {
                    var blocksMiddle = Blocks.Where(m => m.Point.X == block.Point.X
                          && m.Point.Y < block.Point.Y
                          && m.Point.Y > blockLeft.Point.Y);
                    if (blocksMiddle.Count() > 0 && !blocksMiddle.Any(m => m.Status != colorOther))
                    {
                        Count += blocksMiddle.Count();                       
                        foreach (var item in blocksMiddle)
                        {
                            item.Status = colorNow;
                        }
                    }
                }
                //检索上侧
                var blockUp = Blocks.Where(m => m.Point.Y == block.Point.Y
                    && m.Point.X < block.Point.X
                    && m.Status == colorNow).OrderBy(m => m.Point.X).FirstOrDefault();
                if (blockUp != null)
                {
                    var blocksMiddle = Blocks.Where(m => m.Point.Y == block.Point.Y
                          && m.Point.X < block.Point.X
                          && m.Point.X > blockUp.Point.X);
                    if (blocksMiddle.Count() > 0 && !blocksMiddle.Any(m => m.Status != colorOther))
                    {
                        Count += blocksMiddle.Count();                        
                        foreach (var item in blocksMiddle)
                        {
                            item.Status = colorNow;
                        }
                    }
                }
                //检索下侧
                var blockUnder = Blocks.Where(m => m.Point.Y == block.Point.Y
                    && m.Point.X > block.Point.X
                    && m.Status == colorNow).OrderBy(m => m.Point.X).FirstOrDefault();
                if (blockUnder != null)
                {
                    var blocksMiddle = Blocks.Where(m => m.Point.Y == block.Point.Y
                          && m.Point.X > block.Point.X
                          && m.Point.X < blockUnder.Point.X);
                    if (blocksMiddle.Count() > 0 && !blocksMiddle.Any(m => m.Status != colorOther))
                    {
                        Count += blocksMiddle.Count();                        
                        foreach (var item in blocksMiddle)
                        {
                            item.Status = colorNow;
                        }
                    }
                }
                //检索右上侧
                var blockTopRight = Blocks.Where(m => m.Point.X < block.Point.X
                    && m.Point.Y > block.Point.Y
                    && m.Status == colorNow).OrderBy(m => m.Point.Y).FirstOrDefault();
                if (blockTopRight != null)
                {
                    var blocksMiddle = Blocks.Where(m => m.Point.X < block.Point.X
                          && m.Point.X > blockTopRight.Point.X
                          && m.Point.Y > block.Point.Y
                          && m.Point.Y < blockTopRight.Point.Y);
                    if (blocksMiddle.Count() > 0 && !blocksMiddle.Any(m => m.Status != colorOther))
                    {
                        Count += blocksMiddle.Count();                       
                        foreach (var item in blocksMiddle)
                        {
                            item.Status = colorNow;
                        }
                    }
                }
                //检索左上侧
                var blockTopLeft = Blocks.Where(m => m.Point.X < block.Point.X
                    && m.Point.Y < block.Point.Y
                    && m.Status == colorNow).OrderBy(m => m.Point.Y).FirstOrDefault();
                if (blockTopLeft != null)
                {
                    var blocksMiddle = Blocks.Where(m => m.Point.X < block.Point.X
                          && m.Point.X > blockTopLeft.Point.X
                          && m.Point.Y < block.Point.Y
                          && m.Point.Y > blockTopLeft.Point.Y);
                    if (blocksMiddle.Count() > 0 && !blocksMiddle.Any(m => m.Status != colorOther))
                    {
                        Count += blocksMiddle.Count();                       
                        foreach (var item in blocksMiddle)
                        {
                            item.Status = colorNow;
                        }
                    }
                }
                //检索右下侧
                var blockBottomRight = Blocks.Where(m => m.Point.X > block.Point.X
                    && m.Point.Y > block.Point.Y
                    && m.Status == colorNow).OrderBy(m => m.Point.Y).FirstOrDefault();
                if (blockBottomRight != null)
                {
                    var blocksMiddle = Blocks.Where(m => m.Point.X > block.Point.X
                          && m.Point.X < blockBottomRight.Point.X
                          && m.Point.Y > block.Point.Y
                          && m.Point.Y < blockBottomRight.Point.Y);
                    if (blocksMiddle.Count() > 0 && !blocksMiddle.Any(m => m.Status != colorOther))
                    {
                        Count += blocksMiddle.Count();                        
                        foreach (var item in blocksMiddle)
                        {
                            item.Status = colorNow;
                        }
                    }
                }
                //检索左下侧
                var blockBottomLeft = Blocks.Where(m => m.Point.X > block.Point.X
                    && m.Point.Y < block.Point.Y
                    && m.Status == colorNow).OrderBy(m => m.Point.Y).FirstOrDefault();
                if (blockBottomLeft != null)
                {
                    var blocksMiddle = Blocks.Where(m => m.Point.X > block.Point.X
                          && m.Point.X < blockBottomLeft.Point.X
                          && m.Point.Y < block.Point.Y
                          && m.Point.Y > blockBottomLeft.Point.Y);
                    if (blocksMiddle.Count() > 0 && !blocksMiddle.Any(m => m.Status != colorOther))
                    {
                        Count += blocksMiddle.Count();                        
                        foreach (var item in blocksMiddle)
                        {
                            item.Status = colorNow;
                        }
                    }
                }

                //计数               
                if (Count >= 1)
                {
                    if (NextIsBlack)
                    {
                        Num_Black += Count + 1;
                        Num_White -= Count;
                    }
                    else
                    {
                        Num_White += Count + 1;
                        Num_Black -= Count;
                    }
                    Count = 0;
                }                

                //当前block的长方形
                Rectangle rec = new Rectangle
                {
                    Size = new Size((int)unitColWidth, (int)unitRowHeight),
                    Location = new Point((int)((block.Point.Y + 1) * unitColWidth), (int)((block.Point.X + 1) * unitRowHeight))
                };

                if (rec.Contains(point))
                {
                    //point这个点在rec这个矩形里
                    if (block.Status == BlockStatus.Blank)
                    {
                        //只处理空白格子
                        block.MouseDown(NextIsBlack);
                        //切换黑白
                        NextIsBlack = !NextIsBlack;
                    }
                    break;
                }
            }                                           
            return null;//没有点击成功
        }
        
        /// <summary>
        /// 判断游戏结束
        /// </summary>
        /// <returns></returns>
        public int IsGameOver()
        {
            //判断格子被填满，棋子多少        
            var Num_Black = Blocks.Count(m => m.Status == BlockStatus.Black);
            var Num_White = Blocks.Count(m => m.Status == BlockStatus.White);
            if (Num_Black + Num_White == 64)
            {
                if (Num_Black > Num_White)
                    return 0;
                else if (Num_White > Num_Black)
                    return 1;
                else if (Num_Black - Num_White == 0)
                    return 2;
            }                            
            return 3;
        }
    }
}
